package com.lusang.market.service.impl;

import com.lusang.basics.service.impl.BaseServiceImpl;
import com.lusang.market.domain.Business;
import com.lusang.market.domain.BusinessProduct;
import com.lusang.market.domain.Clue;
import com.lusang.market.domain.ClueRemark;
import com.lusang.market.mapper.BusinessMapper;
import com.lusang.market.mapper.BusinessProductMapper;
import com.lusang.market.mapper.ClueMapper;
import com.lusang.market.service.IClueRemarkService;
import com.lusang.market.service.IClueService;
import com.lusang.product.domain.Product;
import com.lusang.basics.util.LoginContext;
import com.lusang.market.dto.ClueBusinessDto;
import com.lusang.market.dto.ClueAndActivityDto;
import com.lusang.org.domain.Employee;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyom
 * @since 2022-08-25
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {
    @Autowired
    ClueMapper mapper;
    @Autowired
    IClueRemarkService clueRemarkService;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    BusinessProductMapper businessProductMapper;
    @Override
    public void association(ClueAndActivityDto cad) {
        List<Long> activityIds = cad.getActivityIds();
        if (activityIds!=null && activityIds.size()>0){
            List<Map<String,Object>> list = new ArrayList<>();
            activityIds.forEach(activityId -> {
                Long clueId = cad.getClueId();
                Map<String,Object> params = new HashMap<>();
                params.put("clueId", clueId);
                params.put("activityId", activityId);
                list.add(params);
            });
            mapper.saveAssoc(list);
        }
    }

    @Override
    public void assign(Clue clue) {
        //分配的本质就是修改数据
        //状态需要改变 变为跟进状态 0表示未分配 1表示跟进 2表示已转化为商机 -1放入线索池
        clue.setState(1);
        mapper.update(clue);
    }

    @Override
    public void business(ClueBusinessDto dto) {
        Employee loginUser = LoginContext.getLoginUser();//当前登陆用户
        Clue clue = dto.getClue();//线索
        Product product = dto.getProduct();//产品
        //修改线索状态
        clue.setState(2);
        //线索备注
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setNoteContent("已转化成商机，操作人员"+ loginUser.getUsername());
        clueRemark.setClue(clue);
        clueRemarkService.add(clueRemark);
        //添加商机，-选择一个产品
        Business business = new Business();
        business.setName(clue.getFullName()+"意向购买"+product.getName());
        business.setClueId(clue.getId());
        business.setProductId(product.getId());
        business.setProductName(product.getName());
        business.setProductPrice(product.getPrice());
        businessMapper.save(business);//返回自增id
        //添加线索产品
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product,businessProduct);
        businessProduct.setId(null); //已经拷贝了id，所以要置空
        businessProduct.setBusinessId(business.getId());//通过上面保存返回的id
        businessProductMapper.save(businessProduct);
    }
}
